Imports Phoenix.DataAccess
Imports Phoenix.Events

Namespace Helpers

    Public Class ServiceHelper : Inherits BaseHelper

#Region " BaseHelper Overrides "
        Private Shared mInstance = New ServiceHelper
        Public Shared ReadOnly Property Instance() As ServiceHelper
            Get
                Return mInstance
            End Get
        End Property


        Private Sub New()

        End Sub

#End Region

#Region " Edit Commands "
        Public Shared Function CreateService() As DataSetData.Service
            Dim srv As DataSetData.Service = AppDataLayer.Services.NewService()
            Tag(srv) = CreateServiceParams()

            srv.Label = Res.NewServiceDefaultLabel

            Return srv
        End Function


        Public Shared Function Copy(ByVal obj As DataSetData.Service) As DataSetData.Service
            Dim s As DataSetData.Service = ServiceHelper.CreateService
            s = Copy(s, obj)
            s.Label = "Copy of " & s.Label
            Return s
        End Function

        Private Shared Function Copy(ByVal s As DataSetData.Service, ByVal obj As DataSetData.Service) As DataSetData.Service
            s.Label = obj.Label
            s.Type = obj.Type
            s.Comment = obj.Comment
            s.Color = obj.Color
            s.TextColor = obj.TextColor
            Return s
        End Function

        Public Shared Sub UpdateService(ByVal Service As DataSetData.Service, ByVal sender As Object)
            If IsNew(Service) Then
                CreateNewServiceCore(Service)
                AppRaiseEvent(sender, New PhoenixEventArgs(Service, PhoenixEventCode.BizAdded))
            Else
                UpdateExistingServiceCore(Service)
                AppRaiseEvent(sender, New PhoenixEventArgs(Service, PhoenixEventCode.BizUpdated))
            End If
        End Sub

        Public Shared Sub DeleteService(ByVal Service As DataSetData.Service, ByVal sender As Object)
            DeleteService(Service, sender, True)
        End Sub

        Public Shared Sub DeleteService(ByVal obj As DataSetData.Service, ByVal sender As Object, ByVal Confirm As Boolean)
            If IsNew(obj) Then Return

            If CloseForm(obj) Then
                If Confirm Then
                    If Not AppConfig.SilenceServiceDeletion Then
                        Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.ServiceDeleteQuestion, obj.Label), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                        If result <> MsgBoxResult.Yes Then Return
                    End If
                End If

                AppRaiseEvent(sender, New PhoenixEventArgs(obj, PhoenixEventCode.BizRemoved))
                Try
                    DeleteServiceCore(obj)
                Catch
                    AppRaiseEvent(sender, New PhoenixEventArgs(obj, PhoenixEventCode.BizAdded))
                End Try
            End If

        End Sub

        Public Shared Sub DeleteServices(ByVal list As IList, ByVal sender As Object)
            If list.Count = 1 Then
                DeleteService(list(0), sender, True)
            Else
                DeleteServices(list, sender, True)
            End If
        End Sub

        Public Shared Sub DeleteServices(ByVal list As IList, ByVal sender As Object, ByVal confirm As Boolean)
            If (Not AppConfig.SilenceServicesDeletion) And confirm Then
                Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.ServicesDeleteQuestion, list.Count), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                If result <> MsgBoxResult.Yes Then Return
            End If

            OperationCanceled = False
            Dim mylist(list.Count - 1) As DataSetData.Service
            list.CopyTo(mylist, 0)
            For i As Long = 0 To list.Count - 1
                DeleteService(mylist(i), sender, False)
                If OperationCanceled Then Return
            Next
        End Sub


#End Region

#Region " CRUD Methods "
        Private Shared Sub CreateNewServiceCore(ByVal Service As DataSetData.Service)
            AppDataLayer.Services.AddService(Service)
            AppDataLayer.UpdateService(Service)

            Dim params As DataSetData.ServiceParam() = Tag(Service)
            For Each param As DataSetData.ServiceParam In params
                param.Service = Service
                AppDataLayer.ServiceParams.AddServiceParam(param)
            Next

            AppDataLayer.UpdateServiceParams(params)
            Tag(Service) = Nothing
        End Sub

        Private Shared Sub UpdateExistingServiceCore(ByVal Service As DataSetData.Service)
            AppDataLayer.BeginTransaction()
            Try
                AppDataLayer.UpdateService(Service)
                AppDataLayer.UpdateServiceParams(Service.GetServiceParams())
            Catch
                AppDataLayer.RollbackTransaction()
                Throw
            End Try
            AppDataLayer.CommitTransaction()
        End Sub

        Private Shared Sub DeleteServiceCore(ByVal service As DataSetData.Service)
            AppDataLayer.BeginTransaction()
            Try
                Dim params As DataSetData.ServiceParam() = service.GetServiceParams()
                For Each param As DataSetData.ServiceParam In params
                    param.Delete()
                    AppDataLayer.UpdateServiceParam(param)
                Next
                service.Delete()
                AppDataLayer.UpdateService(service)
            Catch
                AppDataLayer.RollbackTransaction()
                Throw
            End Try
            AppDataLayer.CommitTransaction()
        End Sub

#End Region

#Region " ServiceParams Helpers "
        Public Shared Function GetServiceParams(ByVal service As DataSetData.Service) As DataSetData.ServiceParam()
            If IsNew(service) Then
                Return CType(Tag(service), DataSetData.ServiceParam())
            Else
                Return service.GetServiceParams()
            End If
        End Function


        Private Shared Function CreateServiceParams() As DataSetData.ServiceParam()
            Dim params(AppDataLayer.MasterLevels.Count - 1) As DataSetData.ServiceParam
            For i As Integer = 0 To params.Length - 1
                Dim param As DataSetData.ServiceParam = AppDataLayer.ServiceParams.NewServiceParam
                param.MasterLevel = AppDataLayer.MasterLevels(i)
                params(i) = param
            Next
            Return params
        End Function
#End Region

#Region "Others Helpers"
        Public Shared Function GetServiceParamByMasterLevel(ByVal Service As DataSetData.Service, ByVal ml As Integer) As DataSetData.ServiceParam
            For Each param As DataSetData.ServiceParam In GetServiceParams(Service)
                If param.MasterLevelID = ml Then Return param
            Next
        End Function
#End Region
    End Class

End Namespace
